/*
 * 
 * 
 * 
 */
package com.exp.base.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.exp.base.entity.SoftDeleteAble;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;


/**
 * Dao - 基类
 * 
 * 
 * 
 */
@Deprecated
@Repository
public class HibernateDao {

    @Resource
    SessionFactory sessionFactory;
	
	public SessionFactory getCurrentSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getCurrentSession(){
		Session session = sessionFactory.getCurrentSession();
		return session;
	}

	
	public <T> void save(T t){
		getCurrentSession().save(t);
	}
	
	public <T> void update(T t){
		getCurrentSession().update(t);
	}
	
	public <T> void saveOrUpdate(T t){
		getCurrentSession().saveOrUpdate(t);
	}
	
	public <T> void delete(T t){
		getCurrentSession().delete(t);
	}
	
	public <T> void softDelete(T t){
		if(t instanceof SoftDeleteAble){
			((SoftDeleteAble)t).setDeleted(true);
			getCurrentSession().update(t);
		}else{
			throw new HibernateException("can't softDelete "+t);
		}
	}
	
	public DetachedCriteria getDetachedCriteria(Class<?>clazz, Map<String,Object> params){
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(clazz);
		for(Entry<String, Object> entry : params.entrySet()){
			detachedCriteria.add(Restrictions.eq(entry.getKey(),entry.getValue()));
		}
		return detachedCriteria;
	}
	
	public Criteria getCriteria(Class<?>clazz, Map<String,Object> params){
		DetachedCriteria detachedCriteria = getDetachedCriteria(clazz, params);
		Criteria criteria = detachedCriteria.getExecutableCriteria(getCurrentSession());
		return criteria;
	}
	
	
	public <T> T get(Class<T> clazz,Serializable id) {
		return (T) getCurrentSession().get(clazz, id);
	}
	public long size(Criteria criteria){
		Long total = ((Long)criteria.setProjection(Projections.rowCount()).uniqueResult());
		if(total==null)total=0L;
		return total;
	}
	public long size(Class<?>clazz, Map<String,Object> params){
		Criteria criteria = getCriteria(clazz,params);
		return size(criteria);
	}
	public boolean exist(Criteria criteria){
		Long total = ((Long)criteria.setMaxResults(1).setProjection(Projections.rowCount()).uniqueResult());
		if(total==null)total=0L;
		return total>0;
	}
	public<T> boolean exist(Class<?>clazz, Map<String,Object> params){
		Criteria criteria = getCriteria(clazz,params);
		return exist(criteria);
	}
	public <T> T find(Criteria criteria) {
		T t =null;
		try {
			t = (T) criteria.uniqueResult();
		} catch (HibernateException e) {
		}
		return t;
	}
	public <T> T find(Class<?>clazz, Map<String,Object> params) {
		Criteria criteria = getCriteria(clazz,params);
		return find(criteria);
	}
	public <T> PageInfo<T> query(Page<T> page,Criteria criteria) {
		if(page==null){
			page = new Page<T>(1,Integer.MAX_VALUE);
		}
		int pageNum = page.getPageNum();
		if(pageNum<1){
			pageNum = 1;
		}
		int startNum = (pageNum-1) * page.getPageSize();
		criteria.setFirstResult(startNum);
		PageInfo<T> pageInfo=null;
		criteria.setMaxResults(page.getPageSize());
		List<T> list = criteria.list();
		if(page.getPageSize()<Integer.MAX_VALUE){
			Long total = ((Long)criteria.setFirstResult(0).setMaxResults(Integer.MAX_VALUE).setProjection(Projections.rowCount()).uniqueResult());
			if(total==null)total=0L;
			page.setTotal(total);
		}else{
			page.setTotal(list.size());
		}
		page.addAll(list);
		pageInfo = new PageInfo<T>(page);
		return pageInfo;
	}
	public <T> PageInfo<T> query(Page<T> page,Class<?>clazz, Map<String,Object> params) {
		Criteria criteria = getCriteria(clazz,params);
		return query(page,criteria);
	}
	
	
	
}